package iscript.uiFramework.components.message 
{
	import flash.display.Bitmap;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import iscript.uiFramework.components.button.BaseButton;
	import iscript.uiFramework.components.container.Box;
	import iscript.uiFramework.components.core.DragManager;
	import iscript.uiFramework.components.layout.HBoxLayout;
	import iscript.uiFramework.components.layout.LayoutAlign;
	import iscript.uiFramework.components.text.TextWord;
	import iscript.uiFramework.components.window.AbstractWindow;
	import iscript.uiFramework.UIFramework; 
	import iscript.uiFramework.util.Scale9GridBitmap;
	public class Message extends  AbstractWindow
	{
		
		protected var header:MessageHeader;
		protected var msgSkin:Scale9GridBitmap;
		private var _textWord:TextWord;
		private var _type:String;
		private var _messageIcon:Bitmap;
		private var _buttonContainer:Box;
		private var _messageButtonConfig:MessageButtonConfig;
		
		public function Message() 
		{
			_preferWidth = 400;
			_preferHeight = 200;
			
			super();
		}
		
		private function _resetPositionLogic(e:Event):void 
		{
			this.x =  UIFramework.UiStage.stageWidth * 0.5 -  this.width * 0.5 ;
			this.y =  UIFramework.UiStage.stageHeight * 0.5 -  this.height * 0.5;
		}
		
		override protected function initialize():void 
		{
			
			_messageButtonConfig = new MessageButtonConfig();
			
			msgSkin = new Scale9GridBitmap(UIFramework.resource.getBitmapData("GameLite.UIComponent." + this.theme + ".Message"), 
											  UIFramework.resource.getElementRectangle("Message", this.theme));
			msgSkin.width = _preferWidth;
			msgSkin.height = _preferHeight;
			addChild(msgSkin);
			
			header = new MessageHeader();
			header.width = _preferWidth;
			header.render(this);
			
		 
			_textWord = new TextWord();
			_textWord.x = 65;
			_textWord.y = 30;			
			_textWord.width = this.width - 65 - 10;
			_textWord.render(this);
			
			text="\n"
			
			  _type =  MessageType.INFO;
			  _messageIcon= new Bitmap(UIFramework.resource.getBitmapData("GameLite.UIComponent.DefaultTheme.Info"));
			  _messageIcon.x = 20;
			  _messageIcon.y = 30;
			  addChild(_messageIcon);
			
			  _buttonContainer = new Box(this.width, this.height)
			  var hbox:HBoxLayout = new HBoxLayout();
			      hbox.vAlign =  LayoutAlign.VAlign_BOTTOM;
			      hbox.hAlign =  LayoutAlign.HAlign_CENTER;
				  hbox.offY = -8;				  
			  _buttonContainer.setLayout(hbox);
			  _buttonContainer.render(this);
			  _initializteMessageButton();
			super.initialize();
		}
		
		override protected function eventListener():void 
		{
			UIFramework.UiStage.addEventListener(Event.RESIZE,_resetPositionLogic)
			header.closeButton.ClickHandler = function():void
			{
				close();	
			}
			super.eventListener();
		}
		
		public function set text(value:String):void {
			_textWord.text = value;
			msgSkin.height = 30 + _textWord.height + 50;
			if (_buttonContainer)
			_buttonContainer.height = this.height;
			_resetPositionLogic(null);
			}
			

			
		override public function set width(value:Number):void {
			 msgSkin.width = value;
			 _textWord.width = msgSkin.width - 65 - 10;
			_buttonContainer.width = value;
			_resetPositionLogic(null);
			}	
			
				
		override 	public function get width():Number {
				return msgSkin.width;
			}
		override 	public function get height():Number {
				return msgSkin.height;
			}
			
			
		public function get text():String {
			return _textWord.text;
			}
		
		public function set title(value:String):void {
			  header.text = value;
			
			}
		public function get title():String {
			return header.text;
			}
		
		
		override public function render(container:DisplayObjectContainer = null):void 
		{
			container = null;
			this.dialog = true;
			this.dialogVisible = true;
		 	this.shadow = true;
			this.x =  UIFramework.UiStage.stageWidth * 0.5 -  this.width * 0.5 ;
			this.y =  UIFramework.UiStage.stageHeight * 0.5 -  this.height * 0.5;
			DragManager.add(this);
			super.render(null);
		}
		
		
		public function get type():String 
		{
			return _type;
		}
		
		/*Message类型，改变图标类型*/
		public function set type(value:String):void 
		{
			_type = value;
			switch(value) {
				 case MessageType.ERROR:
				 _messageIcon.bitmapData = UIFramework.resource.getBitmapData("GameLite.UIComponent.DefaultTheme.Error");
				 break;
				  case MessageType.INFO:
				 _messageIcon.bitmapData = UIFramework.resource.getBitmapData("GameLite.UIComponent.DefaultTheme.Info");
				 break;
				  case MessageType.Question:
				 _messageIcon.bitmapData = UIFramework.resource.getBitmapData("GameLite.UIComponent.DefaultTheme.Question");
				 break;
				  case MessageType.Warn:
				 _messageIcon.bitmapData = UIFramework.resource.getBitmapData("GameLite.UIComponent.DefaultTheme.Warn");
				 break;
				}
		}
		
		/**
		 * 初始化按钮
		 */
		public function _initializteMessageButton():void {
			  
			   
				  var buttonYes:BaseButton = new BaseButton ();
				      buttonYes.width = 100;
					  buttonYes.text = _messageButtonConfig.YesButtonText;
					  buttonYes.ClickAfterHandler = _buttonAfterHandler;
					  buttonYes.ClickHandler = _messageButtonConfig.YesButtonHandler;		 
					  _buttonContainer.append(buttonYes);
 
				  if (_messageButtonConfig.type == MessageButtonType.YES) return;
				  
				  var buttonNo:BaseButton = new BaseButton ();
				      buttonNo.width = 100;
					  buttonNo.text = _messageButtonConfig.NoButtonText;
					  buttonNo.ClickAfterHandler = _buttonAfterHandler;
					  buttonNo.ClickHandler = _messageButtonConfig.NoButtonHandler; 
					 _buttonContainer.append(buttonNo);
				   if (_messageButtonConfig.type == MessageButtonType.YESNO) return;
				   
				  var buttonCancel:BaseButton = new BaseButton ();
				      buttonCancel.width = 100;
					  buttonCancel.text = _messageButtonConfig.CancelButtonText;
					  buttonCancel.ClickAfterHandler = _buttonAfterHandler;
					  buttonCancel.ClickHandler = _messageButtonConfig.CancelButtonHandler;
					  _buttonContainer.append(buttonCancel);
				 

			}
			
			private function _buttonAfterHandler(e:*):void {
				   this.dispose();
				}	
			
		public function get messageButtonConfig():MessageButtonConfig 
		{
			return _messageButtonConfig;
		}
		
		public function set messageButtonConfig(value:MessageButtonConfig):void 
		{
			_messageButtonConfig = value;
			_buttonContainer.clear();
			_initializteMessageButton();
		}
		
		override public function dispose():void 
		{
			if (!Disposed) {
				   DragManager.remove(this);
				   UIFramework.UiStage.removeEventListener(Event.RESIZE, _resetPositionLogic)
				   super.dispose();
				}
		}
		
		
	}

}